const http = require("http")
const URL = require("url")
const obj = {
    name:'张三',
    age:18,
    sex:"男",
    id:66
}
function toHTML(data){
return `
    <ul>
      <li><span>id:</span> <span>${data.id}</span></li>
      <li><span>昵称:</span> <span>${data.name}</span></li>
      <li><span>年龄:</span> <span>${data.age}</span></li>
      <li><span>性别:</span> <span>${data.sex}</span></li>
    </ul>
  `
}

const server = http.createServer((req,res)=>{
    const { pathname } = URL.parse(req.url)             //query是路径         //pathname是查询参数
    if(pathname=="/"){
        const accept = req.headers.accept
        console.log(accept);
        
        if(accept.includes('application/json')){
            res.writeHead(200,{'content-type':"application/json"})
            res.end(JSON.stringify(obj))
        }else{
            res.writeHead(200,{"content-type":"text/html;charset=utf-8"})
            res.end(toHTML(obj))
        }
    }else{
        res.writeHead(404,{'content-type': 'text/html'})
        res.end('<h1>Not Found</h1>')
    }
})

server.listen(3000,()=>{
    console.log("哥们,我把三千端口给你打开了");
    
})